package com.atguigu.jvm.chap06;

import java.util.concurrent.TimeUnit;

/**
 * 强引用：只要存在强引用，那么对象不能被GC
 */
public class StrongReferenceTest {

    public static void main(String[] args) {
        //定义强引用
        User user = new User(1, "zhangsan"); //  GC Root
        //定义强引用
        User user1 = user;

        //设置user为null，User对象不会被回收，因为依然被user1引用
        user = null;

        //强制垃圾回收
        System.gc();

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(user1);
    }
}
